/**
 *  Model with a low level of fear
 *  Author: tom
 *  Description: 
 */

model emotion1
import "Common.gaml"

global{
	float fear_level <- 0.3;
//	float normal_speed <- 0.5;
	float mouvement_speed <- 0.3 min: 0.1 max: 7.0;
	int percent_of_fear <- 0 min: 0 max: 100;
	int nb_people <- 100 min: 1 max: 2000;
	float probability_of_change <- 0.002;
	float max_fear_level <- 0.0;
	float max_human_size <- 2.0;
	int total_collision <- 0;
	
	init {
		create obstacle from : shape_file_obstacles returns: list_of_obstacle ;
		create background from : shape_file_bounds returns: list_of_bg ;
		create port from : shape_file_ports ;
		set obstacles <- obstacles union list ( list_of_obstacle ) ;
		set bg <- first ( list_of_bg ) ;
		ask target : bg {
			set shape <- self - obstacles ;
		}
		let ports type : list of : port <- list ( port ) ;		
		
		
		create people number : nb_people {
			set location <- any_location_in ( bg ) ;
			if(flip(percent_of_fear/100.0)){
				set fear_level <- max_fear_level;
				//normal state
				if(fear_level < 0.1){
					set speed <- mouvement_speed;
				}else if (fear_level < 0.3){//phobia
					set speed <- mouvement_speed * 2 ;//level 1, speed = 0.6
				}else if (fear_level < 0.6){
					set speed <- mouvement_speed * 3;//level 2, speed = 0.9
				}else if (fear_level < 0.8){//level 3
					set speed <- mouvement_speed * 5;			//speed = 1.5
				}else{							//panic
					set speed <- mouvement_speed * 7;			//speed = 2.1
				}								
			}else{
				set speed <- mouvement_speed;
			}
			//set the_target <- first ( ports ) ;
		}

	}
}

entities {
	species people skills: [moving]{
		float size <- max_human_size;
		rgb color <- rgb('yellow');
		point tar <- nil;
		point prev <- nil;
		aspect base {
			draw shape : circle size : size color : color ;
		}
		/**
		 * quand les agents n'ont pas peur, ils déplacent lentement ou ne déplacent pas 
		 */
		reflex on_target when: tar != nil {
			if(self distance_to tar < size){
				set tar <- nil;				
			}
		}
		
		reflex do_wander when: tar = nil{
			do wander speed: speed bounds: bg;

			if(flip(probability_of_change)){
				let pos <- any_location_in(bg);
				set tar <- pos;
			}			
		}
		
		reflex flee_people{
			//quand il y a des collisions, les vitesses des agents ralentissent
			//et peut-etre = 0
			//let closes type: list of: people <- people overlapping(self);
			let distance_to_flee <- 1.5 * max_human_size;	
			let closes type: list of: people <-  (self neighbours_at distance_to_flee of_species people) sort_by (self distance_to each);			
			loop close over: closes{
				let hd <- self towards close - rnd(60) - 40; 
				//do move speed: speed heading: hd bounds: bg;
			}
			if(not empty(closes)){
				//do move heading: self towards first(closes) - 100 bounds: bg;
				set heading <- self towards first(closes) - 100;
				if(tar = nil){
					do move heading: heading bounds: bg;
				}
			}else if(tar != nil){
				set heading <- self towards tar;
			}
			
		}
		
		reflex to_target when: tar != nil{
			let new_prev <- location;
			//prev <- location;
			//do move heading: self towards tar speed: speed bounds: bg;
			do move heading: heading speed: speed bounds: bg;
			if(circle(mouvement_speed/3) overlaps prev){
				set tar <- nil;
			}
			set prev <- new_prev;			
			//do wander speed: normal_speed bounds: bg;
		}
		
		reflex collision{
			//let collision type: list of: people <- (self );
			
			let distance_collision <- 0.95 * max_human_size;
			let closes type: list of: people <-  (self neighbours_at distance_collision of_species people);
			set total_collision <- total_collision + length(closes);
		}
		
	}

}

experiment Emotion4 type: gui {
	//parameter 'Vitesse des agents m/s:' var: mouvement_speed category: 'Model' ;
	parameter 'Niveau de peur:' var: max_fear_level category: 'Model' ;
	parameter 'Pourcentage d\'agent ayant peur :' var: percent_of_fear category: 'Model' ;
	output {
		inspect name:'Agent' type: agent refresh_every: 1;
		inspect name:'Species' type:species refresh_every: 1;			
		
		display building_display refresh_every: 1 {		
			species background aspect : base;
			species obstacle aspect: base ;
			species port aspect: base ;
			species people aspect: base ;								
		}
		monitor name:"Collision:" value: total_collision as int;
	}	
}